<?php if ( ! defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );
/**
 * conlabzCMS
 *
 * A website backend system for developers for PHP 4.3.2 or newer
 *
 * @package         conlabz CMS
 * @author          Christian Schneider
 * @copyright       Copyright (c) 2009
 * @license        
 * @link            http://www.conlabz.de
 * @filesource
 */

// ---------------------------------------------------------------------------

/**
 * Corporate
 *
 * Allow the user to manage website legal notice
 *
 * @package         conlabz CMS
 * @subpackage      Controllers
 */
class Corporate extends User_Controller {
	function Corporate( ) {
		parent::User_Controller( );

		$this->load->helper( 'form' );

		// Set breadcrumb
		$this->bep_site->set_crumb( $this->lang->line( 'corporate' ), 'article/user/corporate' );

		// Check for access permission
		// needs to be defined as a resource over the admin area
		check( 'Corporate' );

		// Load the validation library
		$this->load->library( 'validation' );

		// Load the corporate model
		$this->load->model( 'corporate_model' );
		$this->load->model( 'website_model' );
		
		// Load the corporate language module
		$this->lang->module_load( 'article', 'corporate' );
		
		log_message( 'debug', 'conlabz CMS : Corporate class loaded' );
	}

	/**
	 * View Corporate
	 *
	 * @access public
	 */
	function index( ) {
		// Get Article Infomation
		$data['corporate'] = $this->corporate_model->getCorporate( );
		
		// Display Page
		$data['header'] = $this->lang->line( 'corporate_overview' );
		$data['page']   = $this->config->item( 'backendpro_template_user' ) . "/view_corporate";
		$data['menu'] = $this->website_model->getWebsiteUserMenu( );
		$data['module'] = 'article';
		$this->load->view( $this->_container, $data );
	}

	/**
	 * Get Corporate Details
	 *
	 * Load article detail values from the submitted form
	 *
	 * @access private
	 * @return array
	 */
	function _get_corporate_details( ) {
		$data['company'] 				   = $this->input->post( 'company'   );
		$data['name']    				   = $this->input->post( 'name'      );
		$data['extension']    			   = $this->input->post( 'extension' );
		$data['address']    			   = $this->input->post( 'address'   );
		$data['zipcode']                   = $this->input->post( 'zipcode'   );
		$data['city']                      = $this->input->post( 'city'      );
 		$data['phone']    				   = $this->input->post( 'phone'     );
		$data['trade_id']    			   = $this->input->post( 'trade_id'  );
		$data['vat_id']    				   = $this->input->post( 'vat_id'    );
		$data['mail']    				   = $this->input->post( 'mail'      );
		$data['internet']    			   = $this->input->post( 'internet'  );
		$data['information']               = $this->input->post( 'information' );
		$data['authorised_representative'] = $this->input->post( 'authorised_representative' );

		return $data;
	}

	/**
	 * Display Corporate Form
	 *
	 * @access public
	 */
	function form( ) {
		$fields['information'] = $this->lang->line( 'corporate_information' );
		$this->validation->set_fields( $fields );
		
		// Setup form default values
		if( !$this->input->post( 'submit' ) ) {
			// Modify form, first load
			$data['corporate'] = $this->corporate_model->getCorporate( );
			
			$data['fckeditor'] = array(
              	'name'        => 'description',
              	'id'          => 'description',
              	'toolbarset'  => 'Default',
              	'basepath'    => DEVPATH.'assets/plugins/fckeditor/',
              	'width'       => '60%',
              	'height'      => '300',
				'config'	  => '/template/'.$this->session->userdata( 'template' ).'/js/fckeditor.js'
    		);
    		
			// Display form
			$data['header'] = $this->lang->line( 'update_corporate' );
			$this->bep_site->set_crumb( $data['header'], 'article/user/corporate/form/' );
			$data['page']   = $this->config->item( 'backendpro_template_user' ) . "/form_corporate";
			$data['menu'] = $this->website_model->getWebsiteUserMenu( );
			$data['module'] = 'article';
			$this->load->view( $this->_container, $data );
		} else {
			// UPDATE
			$corporate = $this->_get_corporate_details( );
			
			$this->db->trans_start( );
			$this->corporate_model->update( $corporate );

			if( $this->db->trans_status( ) === TRUE ) {
				$this->db->trans_commit( );
				flashMsg( 'success', sprintf( $this->lang->line( 'article_saved' ), 'Impressum' ) );
			} else {
				$this->db->trans_rollback( );
				flashMsg( 'error', sprintf( $this->lang->line( 'backendpro_action_failed' ), $this->lang->line( 'corporate_edited' ) ) );
			}
			redirect( 'article/user/corporate' );
		}
	}
}
/* End of file corporate.php */
/* Location: ./modules/article/controllers/user/corporate.php */
